<?php


namespace App\Admin\Extensions;

use App\Models\PurchaseToMaterial;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid\Displayers\AbstractDisplayer;
use Illuminate\Database\Eloquent\Builder;

class MyStock extends AbstractDisplayer
{
    public function display()
    {
        $id = $this->getColumn()->getOriginal();

        $number = PurchaseToMaterial::whereHas('purchase',function (Builder $query) {
            $query->where('store_id', Admin::user()->store_id)
	            ->where('status', 2);
        })->where('material_id', $id)
	        ->where('status', 1)
	        ->sum('stock');
        
        return $number;
    }
}
